package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {


    @Autowired
    CategoryService categoryService;


    /**
     * 根据类型查询分类
     * 分类类型：1为菜品分类，2为套餐分类
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> selectByType(Integer type){
        List<Category> categories = categoryService.selectByType(type);
        log.info("根据type查询分类数据:{}",categories);
        return Result.success(categories);
    }

    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> selectByPage(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.selectByPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO ){

        categoryService.updateById(categoryDTO);

        return Result.success();
    }

    @ApiOperation("修改分类状态")
    @PostMapping("/status/{status}")
    public Result updateStatusById(@PathVariable Integer status,Long id){

        categoryService.setStatusById(status,id);

        return Result.success();
    }

    @ApiOperation("新增分类")
    @PostMapping
    public Result insert( @RequestBody CategoryDTO categoryDTO ){

        categoryService.insert(categoryDTO);

        return Result.success();
    }


    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result insert(Integer id ){

       categoryService.deleteById(id);

        return Result.success();
    }

}
